﻿using System;
using System.Windows.Data;
using Skyper.Model;
using System.Windows.Media.Imaging;
using System.Windows;


namespace Skyper.Data {
	public sealed class UserStatusImageConverter : IValueConverter {
		#region Public
		/// <summary>
		/// Modifies the source data before passing it to the target for display in the UI.
		/// </summary>
		/// <param name="value">The source data being passed to the target.</param>
		/// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the target dependency property.</param>
		/// <param name="parameter">An optional parameter to be used in the converter logic.</param>
		/// <param name="culture">The culture of the conversion.</param>
		/// <returns>
		/// The value to be passed to the target dependency property.
		/// </returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (value != null && (value is UserStatus || value is ContactModel)) {
				UserStatus status = UserStatus.Offline;
				if (value is UserStatus) {
					status = (UserStatus)value;
				} else if (value is ContactModel) {
					status = ((ContactModel)value).Status;
				}
				Uri source = null;

				Int32 size = 32;
				if (parameter != null){
					if (parameter is Int32) {
						size =  (Int32)parameter;
					} else if (parameter is Double) {
						size =  (Int32)(Double)parameter;
					} else if (parameter is String) {
						if (Int32.TryParse(parameter as String, out size)) {
							
						}
					}
					switch (size) {
						case 16:
						case 24:
						case 32:
						case 48:
						case 128:
							break;
						default:
							size = 32;
							break;
					}
				}
				String vSize = String.Format("{0}x{0}",size);
				switch (status) {
					default:
					case UserStatus.Unknown:
					case UserStatus.Offline:
						source = new Uri("/Images/UserPresence/PresenceOffline_"+vSize+".png", UriKind.Relative);
						break;
					case UserStatus.Online:
						source = new Uri("/Images/UserPresence/PresenceOnline_" + vSize + ".png", UriKind.Relative);
						break;
					case UserStatus.Away:
						source = new Uri("/Images/UserPresence/PresenceAway_" + vSize + ".png", UriKind.Relative);
						break;
					case UserStatus.DoNotDisturb:
						source = new Uri("/Images/UserPresence/PresenceDoNotDisturb_" + vSize + ".png", UriKind.Relative);
						break;
					case UserStatus.NotAvailable:
						source = new Uri("/Images/UserPresence/PresenceAway_" + vSize + ".png", UriKind.Relative);
						break;
					case UserStatus.SkypeMe:
						source = new Uri("/Images/UserPresence/PresenceOnline_" + vSize + ".png", UriKind.Relative);
						break;

				}

				try {
					var img = new BitmapImage(source);

					return img;
				} catch {
					if (System.Diagnostics.Debugger.IsAttached) {
						System.Diagnostics.Debugger.Break();
					}
				}
			}

			return null;
		}
		/// <summary>
		/// Modifies the target data before passing it to the source object.  This method is called only in <see cref="F:System.Windows.Data.BindingMode.TwoWay"/> bindings.
		/// </summary>
		/// <param name="value">The target data being passed to the source.</param>
		/// <param name="targetType">The <see cref="T:System.Type"/> of data expected by the source object.</param>
		/// <param name="parameter">An optional parameter to be used in the converter logic.</param>
		/// <param name="culture">The culture of the conversion.</param>
		/// <returns>
		/// The value to be passed to the source object.
		/// </returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			throw new NotImplementedException();
		}
		#endregion
	}
}
